package api

import (
	"context"
	"github.com/gin-gonic/gin"
	"kuangstudy-mall/apis/userrelation-web/microservices"
	context2 "kuangstudy-mall/apis/userrelation-web/models/context"
	"kuangstudy-mall/apis/userrelation-web/proto"
	"kuangstudy-mall/apis/userrelation-web/validation"
)

type UserAddressApi struct {
	BaseApi
}

/*
*
查询用户收货地址
*/
func (api *UserAddressApi) FindUserAddressList(ctx *gin.Context) {
	userId := ctx.GetUint64("userId")
	defaultRequest := proto.DefaultRequest{
		UserId: userId,
	}
	addressListResponse, err := microservices.GetNacosUserAddressClient().FindUserAddressList(context.Background(), &defaultRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(addressListResponse, ctx)
}

/*
*
创建收货地址
*/
func (api *UserAddressApi) CreateUserAddress(ctx *gin.Context) {
	userAddressCreateContext := context2.UserAddressCreateContext{}
	err := ctx.ShouldBindJSON(&userAddressCreateContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userAddressCreateContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	addressRequest := proto.AddressRequest{
		UserId:      userId,
		Province:    userAddressCreateContext.Province,
		City:        userAddressCreateContext.City,
		District:    userAddressCreateContext.District,
		DefaultFlag: userAddressCreateContext.DefaultFlag,
		Address:     userAddressCreateContext.Address,
		Username:    userAddressCreateContext.Username,
		Mobile:      userAddressCreateContext.Mobile,
	}

	addressResponse, err := microservices.GetNacosUserAddressClient().CreateUserAddress(context.Background(), &addressRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(addressResponse, ctx)
}

/*
*修改收货地址信息
 */
func (api *UserAddressApi) UpdateUserAddress(ctx *gin.Context) {
	userAddressCreateContext := context2.UserAddressCreateContext{}
	err := ctx.ShouldBindJSON(&userAddressCreateContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userAddressCreateContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	addressRequest := proto.AddressRequest{
		Id:          userAddressCreateContext.Id,
		UserId:      userId,
		Province:    userAddressCreateContext.Province,
		City:        userAddressCreateContext.City,
		District:    userAddressCreateContext.District,
		Address:     userAddressCreateContext.Address,
		Username:    userAddressCreateContext.Username,
		Mobile:      userAddressCreateContext.Mobile,
		DefaultFlag: userAddressCreateContext.DefaultFlag,
	}

	_, err = microservices.GetNacosUserAddressClient().UpdateUserAddress(context.Background(), &addressRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(true, ctx)
}

/*
*删除收货地址信息
 */
func (api *UserAddressApi) DeleteUserAddress(ctx *gin.Context) {
	userAddressDeleteContext := context2.UserAddressDeleteContext{}
	err := ctx.ShouldBindJSON(&userAddressDeleteContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userAddressDeleteContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	deletetRequest := proto.DeletetRequest{
		Id:     userAddressDeleteContext.Id,
		UserId: userId,
	}

	_, err = microservices.GetNacosUserAddressClient().DeleteUserAddress(context.Background(), &deletetRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(true, ctx)
}

/*
*获取与用户默认收货地址信息
 */
func (api *UserAddressApi) GetUserAddressByDefault(ctx *gin.Context) {
	userId := ctx.GetUint64("userId")
	defaultRequest := proto.DefaultRequest{
		UserId: userId,
	}
	addressResponse, err := microservices.GetNacosUserAddressClient().GetUserAddressByDefault(context.Background(), &defaultRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(addressResponse, ctx)
}

/*
*设置默认收货地址
 */
func (api *UserAddressApi) SettingUserAddressByDefault(ctx *gin.Context) {
	userAddressDeleteContext := context2.UserAddressDeleteContext{}
	err := ctx.ShouldBindJSON(&userAddressDeleteContext)
	if err != nil {
		api.FailCodeMsgAny(603, validation.GetErrors(err, userAddressDeleteContext), ctx)
		return
	}
	userId := ctx.GetUint64("userId")
	settingDefaultRequest := proto.SettingDefaultRequest{
		Id:     userAddressDeleteContext.Id,
		UserId: userId,
	}

	_, err = microservices.GetNacosUserAddressClient().SettingUserAddressByDefault(context.Background(), &settingDefaultRequest)
	if err != nil {
		api.handlerGrpcError(err, ctx)
		return
	}
	api.OK(true, ctx)
}
